//
//  ShellLanguageViewController.m
//  ShellWallert
//
//  Created by lvzhao on 2019/5/14.
//  Copyright © 2019 吕VV. All rights reserved.
//

#import "ShellLanguageViewController.h"
#import "LZBaseTableView.h"
#import "ShellLanguageCell.h"
@interface ShellLanguageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) LZBaseTableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataArray;
@property (nonatomic ,strong) NSString *selectContent;

@end

@implementation ShellLanguageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = COLOR_BACKGROUND;
    self.title = LZLocalizedString(@"世界语言");
    [self.dataArray addObject:@"繁体中文"];
    [self.dataArray addObject:@"English"];
    [self.dataArray addObject:@"한국어"];
    [self.dataArray addObject:@"日語"];
    //获取手机本地的语言
    
    NSString *currentLanguage = LZToolCache.locationLanguge;
    if([currentLanguage  isEqualToString:@"zh-HK"]){
        self.selectContent = @"繁体中文";
    } else if([currentLanguage containsString:@"ja"]){
        self.selectContent = @"日語";
    } else if([currentLanguage containsString:@"ko"]){
        self.selectContent = @"한국어";
    } else {
        self.selectContent = @"English";
    }
    
    [self setupUI];

}

#pragma mark - setupUI
- (void)setupUI{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.right.bottom.mas_equalTo(0);
    }];
    self.tableView.tableFooterView = [UIView new];
    
    
    
    ///!!!:完成
    UIButton *finishBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"完成") titleColor:UIColorHex(0xFFFFFF) fontSize:14];
    [self initBarItem:finishBtn withType:1];
    
    [[finishBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [kShellLanguageManager setUserlanguage:self.selectContent];
        [LZTool.currentViewController.navigationController popViewControllerAnimated:YES];
    }];
    
    
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShellLanguageCell *languageCell = [tableView dequeueReusableCellWithIdentifier:@"ShellLanguageCell"];
    
    NSString *title = self.dataArray[indexPath.row];
    languageCell.modelObject = title;
    if([title isEqualToString:self.selectContent]){
        languageCell.selectImgView.hidden =  NO;
    } else {
        languageCell.selectImgView.hidden =  YES;
    }
    
    return languageCell;
    
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    self.selectContent = title;
    [self.tableView reloadData];
}


#pragma mark - actions
- (void)saveClick:(UIButton *)sender
{
//    switch (self.enterType) {
//        case MOSettingEnterTypeLanguage:{
//            //修改本地的存值
//            [kMOLanguageManager setUserlanguage:self.selectContent];
//        }
//
//            break;
//        case MOSettingEnterTypeSex:{
//            //先上传服务器. 修改本地的值.
//            //pop上个页面
//            NSInteger index = [self.dataArray indexOfObject:self.selectContent];
//            self.selectContent = [NSString stringWithFormat:@"%ld",index + 1];
//        }
//            break;
//
//        default:
//
//            break;
//    }
//
//    if(self.subject){
//        [self.subject sendNext:self.selectContent];
//    }
//
//    [self.navigationController popViewControllerAnimated:YES];
    
}
#pragma mark - custool

#pragma mark - get && set
- (LZBaseTableView *)tableView{
    if(!_tableView){
        _tableView = [[LZBaseTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        _tableView.backgroundColor = COLOR_BACKGROUND;
        _tableView.rowHeight = 55;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ShellLanguageCell class] forCellReuseIdentifier:@"ShellLanguageCell"];
    }
    return _tableView;
}
- (NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
